﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.Configuration
{
    public partial class AddCatagory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateCatagories();
            }
        }

        private void PopulateCatagories()
        {
            try
            {
                CatagoryManager manager = new CatagoryManager();
                List<Catagory> catagories = new List<Catagory>();
                catagories = manager.GetAllCatagories();
                EmpCatagoryGridview.DataSource = catagories;
                EmpCatagoryGridview.DataBind();

            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void SaveCatagory(object sender, EventArgs e)
        {
            try
            {
                Catagory aCatagory = new Catagory();
                CatagoryManager manager = new CatagoryManager();
                aCatagory.EmpCatagory = txtAddCatagory.Value;
                string msg = manager.Save(aCatagory);
                msgLabel.Text = msg;
                if (msg.Contains("Successfully"))
                {
                    ClearInputs();
                    msgLabel.ForeColor = Color.Green;
                    msgLabel.Font.Bold = true;
                    txtAddCatagory.Focus();
                }
                else
                {
                    msgLabel.ForeColor = Color.Red;

                }
                PopulateCatagories();

            }
            catch (Exception ex)
            {

                msgLabel.ForeColor = Color.Red;
                msgLabel.Text = "Please Add one Catagory Name";
            }

        }

        private void ClearInputs()
        {
            txtAddCatagory.Value = "";

        }

        protected void EmpCatagoryGridview_RowEditing(object sender, GridViewEditEventArgs e)
        {
            EmpCatagoryGridview.EditIndex = e.NewEditIndex;
            PopulateCatagories();
        }

        protected void EmpCatagoryGridview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = true;
            EmpCatagoryGridview.EditIndex = -1;
            PopulateCatagories();
        }

        protected void EmpCatagoryGridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                int catId = Convert.ToInt32(((Label)EmpCatagoryGridview.Rows[e.RowIndex].FindControl("txtId")).Text);
                string cat = ((TextBox)EmpCatagoryGridview.Rows[e.RowIndex].FindControl("txtCatagory")).Text;
                CatagoryManager manager = new CatagoryManager();
                msgLabel.Text = manager.UpdateCatagory(catId, cat);
                EmpCatagoryGridview.EditIndex = -1;
                PopulateCatagories();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void EmpCatagoryGridview_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {


                CatagoryManager manager = new CatagoryManager();
                int catId = Convert.ToInt32(((Label)EmpCatagoryGridview.Rows[e.RowIndex].FindControl("txtId")).Text);
                msgLabel.Text = manager.DeleteCatagory(catId);
                EmpCatagoryGridview.EditIndex = -1;
                PopulateCatagories();
            }
            catch (Exception ex)
            {
                msgLabel.Text = ex.Message;
                msgLabel.ForeColor = Color.Red;



            }

        }

        protected void EmpCatagoryGridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            EmpCatagoryGridview.PageIndex = e.NewPageIndex;
            PopulateCatagories();
        }

    }
}